<?php
	global $SSO, $Security, $AppID, $User, $DB;
	//Check to see if there is a valid session 
	$SessionInfo = $SSO->isValidSession("index.php?action=login", $SSO->getSessionInformation());	
		
	//Check to see if the user has rights to be on LivingWeb
	$PageRights = array('ADMIN', 'USER');
	$UserRoles = $Security->ValidateUser($SessionInfo['username'], $AppID, $PageRights, 1);
	
	//Grab the user info
	$UserInfo = $User->GetUserInfo($SessionInfo['username']);	
	
	$array_of_divs = explode(",",$UserInfo['div_codes']);
	if(trim($UserInfo['div_codes']) == "" && !in_array("ADMIN",$UserRoles))
	{
		print '<script>alert(\'You do not have access to any ministries.\nContact your adminsitrator if you believe this to be an error.\');history.back(-1);</script><noscript><span class="T1">You do not have access to any ministries.  Contact your adminsitrator if you believe this to be an error.</span></noscript>';
		exit();
	}
	elseif(!in_array($_GET['div_code'],$array_of_divs) && !in_array("ADMIN",$UserRoles))
	{
		print '<script>alert(\'You do not have access to this ministry.\nContact your adminsitrator if you believe this to be an error.\');history.back(-1);</script><noscript><span class="T1">You do not have access to this page.  Contact your adminsitrator if you believe this to be an error.</span></noscript>';
		exit();
	}
	else
	{
		if(isset($_GET['div_code']))
		{
			$DB->query("SELECT MinistryName FROM ministries WHERE MinistryID = '" .  $_GET['div_code'] . "'");
			$ministry_row = $DB->fetch_row();
		}
		if(isset($_GET['PID']))
		{
			$DB->query("SELECT * FROM pages WHERE PageID = '" . $_GET['PID'] . "'");
			$row = $DB->fetch_row();
			if($row['MainPage'] != 1)
			{
				$DB->query("DELETE FROM pages WHERE PageID = '" . $_GET['PID'] . "'");
				$DB->query("DELETE FROM page_content WHERE PageID = '" . $_GET['PID'] . "'");
			}
			else
			{
				print '<script>alert(\'You cannot delete a main page.\');history.back(-1);</script><noscript><span class="T1">You cannot delete a main page.</span></noscript>';
				exit();
			}
		}
	}
	?>

<table cellpadding="0" cellspacing="0" align="center" width="70%">
	<tr bgcolor="#D8DA85">
		<td style="border-bottom:1px solid #000000;"><span class="TB2">&nbsp;<?php print $ministry_row['MinistryName']; ?></span></td>
		<td style="border-bottom:1px solid #000000;" valign="middle" align="right">
			<table cellpadding="3" cellspacing="3">
				<tr>
					<td><span class="T1">&nbsp;</span></td>
				</tr>
			</table>
		</td>
	</tr>
	<tr bgcolor="#FEFFCD">
		<td style="border-bottom:1px solid #000000;"><span class="T1">&nbsp;&nbsp;<em>New Page</em></span></td>
		<td style="border-bottom:1px solid #000000;" valign="middle" align="right">
			<table cellpadding="3" cellspacing="3">
				<tr>
					<td>
						<a class="L0" href="index.php?action=new&MID=<?php print $_GET['div_code'] ?>">New Page</a></td>
				</tr>
			</table>
		</td>
	</tr>
<?php
	global $PageServer;
	$pages = $PageServer->GetPagesByMinistryID($_GET['div_code']);
	
	foreach($pages['pages'] as $id => $title)
	{
		print '
		<tr>
			<td style="border-bottom:1px solid #000000;"><span class="T1">&nbsp;&nbsp;' . $title . '&nbsp;&nbsp;<em>(PID ' . $id . ')</em>' . '</span></td>
			<td style="border-bottom:1px solid #000000;" valign="middle" align="right">
				<table cellpadding="3" cellspacing="3">
					<tr>
						<td>
							<a class="L0" href="index.php?action=edit&PID=' . $id . '">Edit</a></td>
						<td>
							<span class="T0">|</span></td>
						<td>
							<a class="L0" href="javascript:if(confirm(\'Delete this page?\')){location.href=\'index.php?action=pages_by_div&div_code=' . $_GET['div_code'] . '&PID=' . $id . '\';}">Delete</a></td>
					</tr>
				</table>
			</td>
		</tr>';
	}
?>
</table>
